# 1、文件读取

# 打开文件，r: 读取，w:写入
file = open(file="../data/students.txt", mode="r", encoding="UTF-8")

# 读取所有行，返回一个列表
students = file.readlines()

print(type(students), students)

# 去点数据中的换行符
students = [student.strip() for student in students]
print(students)

# 关闭文件
file.close()

# 使用with读取，会自动关闭文件
# w：覆盖写入，a:追加写入
with open(file="../data/students.txt", mode="r", encoding="UTF-8") as file:
    # 读取所有行，去掉换行符
    stus = [stu.strip() for stu in file.readlines()]

print(stus)

# 2、将数据保存到文件中
with open(file="../data/stus.txt", mode="a", encoding="UTF-8") as file:
    for stu in stus:
        file.write(stu)
        # 换行
        file.write("\n")
